﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_FriendLink
{
    public partial class Edit : AdminPage
    {
        private FriendLink curFriendLink = new FriendLink();
        private FriendLinkClass curFriendLinkClass = new FriendLinkClass();
        //private Dictionary<string, string> dic = null;
        private string id = "";
        private string classno = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            id = (Request.QueryString["id"] == null) ? "" : Request.QueryString["id"].Trim();
            classno = (Request.QueryString["clsno"] == null) ? "" : Request.QueryString["clsno"].Trim();
            if (!IsPostBack)
            {
                //上次请求路径
                if (hidReferrer.Value.Trim() == "")
                {
                    Uri uri = Request.UrlReferrer;
                    if (uri != null)
                    {
                        hidReferrer.Value = uri.ToString();
                    }
                }
                DdlMenu_bind();
                InitData();
            }
        }

        #region 类别数据绑定
        void DdlMenu_bind()
        {
            DataTable dt = curFriendLinkClass.GetList();
            this.DdlMenu.Items.Clear();
            this.DdlMenu.Items.Add(new ListItem("请选择所属分类", ""));
            int tCount = 1;
            foreach (DataRow dr in dt.Rows)
            {
                string clsno = dr["clsno"].ToString();
                string clsname = dr["clsname"].ToString();
                ListItem li = new ListItem(clsname, clsno);
                DdlMenu.Items.Add(li);
                if (clsno == classno)
                {
                    DdlMenu.SelectedIndex = tCount;
                }
                tCount++;
            }
        }
        #endregion

        private void InitData()
        {

            if (id != "")
            {
                int tid = int.Parse(id);
                FriendLinkInfo model = curFriendLink.GetFriendLinkInfo(tid);
                if (model != null)
                {
                    //hidNO.Value = model.id.ToString();
                    txtTargetUrl.Text = model.targeturl;
                    txtDescription.Text = model.description;
                    txtContent.Text = model.text;
                }
                else
                {
                    Response.Redirect("../Message.aspx?message=修改链接失败，未找到相对应的链接！&z=no");
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=修改链接失败，未从正确的地址编辑！&z=no");
            }
        }

        protected void btnModify_Click(object sender, EventArgs e)
        {
            string classno = this.DdlMenu.SelectedValue.Trim();
            if (classno != "")
            {
                if (id != "")
                {
                    FriendLinkInfo model = new FriendLinkInfo();
                    int tid = int.Parse(id);
                    model.id = tid;
                    model.targeturl = txtTargetUrl.Text.Trim();
                    model.clsno = classno;
                    model.description = txtDescription.Text.Trim();

                    model.type = int.Parse(rdolstTarget1.SelectedItem.Value);
                    model.text = txtContent.Text.Trim();
                    string images_path = "";
                    if (fu_images.PostedFile.ContentLength > 1)
                    {
                        ChangeHope.Common.UploadFile uf = new ChangeHope.Common.UploadFile();
                        uf.ExtensionLim = ".gif,.jpg,.jpeg,.dmp";
                        uf.FileLengthLim = 1024;
                        uf.PostedFile = this.fu_images;
                        uf.SavePath = "/yxuploadfile/accessories/advertise";
                        string errorInfo = "";

                        if (uf.Upload())
                        {
                            if (uf.HaveLoad)
                            {
                                if (!string.IsNullOrEmpty(id) && ViewState["Images"] != null)
                                {
                                    ChangeHope.Common.FileHelper fh = new ChangeHope.Common.FileHelper();
                                    fh.DeleteFile(Server.MapPath("~\\" + ViewState["Images"].ToString()));
                                }
                                images_path = uf.FilePath;
                            }
                            else
                            {

                                errorInfo = uf.Message;
                            }
                        }
                        else
                        {
                            errorInfo = uf.Message;
                        }
                        if (images_path == "")
                        {
                            return;
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(id) && ViewState["Images"] != null)
                        {
                            images_path = ViewState["Images"].ToString();
                        }
                    }

                    model.image = images_path;
                    if (curFriendLink.Update(model))
                    {
                        string refurl = hidReferrer.Value.Trim();
                        if (refurl == "" || refurl == this.Request.Path.Trim())
                        {
                            Response.Redirect("../Message.aspx?message=修改链接成功！" + "&z=yes");
                        }
                        else
                        {
                            Response.Redirect(refurl);
                        }
                    }
                    else
                    {
                        Response.Redirect("../Message.aspx?message=修改链接失败！&z=no");
                    }
                }
                else
                {
                    Response.Redirect("../Message.aspx?message=修改链接失败，无效编辑！&z=no");
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=修改链接失败，请选择要添加链接的页面！&z=no");
            }
        }
    }
}
